window.onload = function () {
    var btns = document.getElementsByClassName('num');
    var operators = ['+', '-', '*', '/']
    var setInputEl = document.getElementById('setInput')
    var resultInputEl=document.getElementById('resultInput');
    var inputValue = 0;
    for (var i = 0; i < btns.length; i++) {
        btns[i].addEventListener('click', function () {
            var ops = this.getAttribute('data-ops');
            if (ops != 'equa') {
                inputValue = document.getElementById('setInput').value;
                setInputEl.value = inputValue + this.innerText;
            }
            
            if (ops == 'clear') {
                if (inputValue != '') {
                    setInputEl.value = '';
                    resultInputEl.value=''
                }
            }
            if (ops == 'equa') {
                //不必把等号加入输入内容   
            getResult();
            }
        })
    };
    function getResult() { //点击等号，计算结果
        var equation = setInputEl.value;
        var lastChar = equation[equation.length - 1];
        equation = equation.replace(/x/g, '*').replace(/÷/g, '/');
        if (operators.indexOf(lastChar) > -1) { //输入的最后一位字符串不是运算符，处理为空
            equation = equation.replace(/.$/g, '')
        }
        if (equation) {
            resultInputEl.value=eval(equation)
        }
    }
}